/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import CBOR from "./cborjs.js";
import { data } from "./data.js";

let encoded;

// Encode data using concise binary object CBOR.
function encoded_test() {
    const startTime1 = Date.now()
    // for (let i = 0; i < 500; i++) {
        encoded = CBOR.encode(data);
    // }
    // print(`encoded.byteLength = ${encoded.byteLength}`)
    // print(`encoded result ${encoded}`)
    if (encoded == null) {
        throw new Error('Inconsistent with expected results')
    }
    const endTime1 = Date.now()
    print(`cborjs_Encode: ${endTime1 - startTime1} ms`)
}

// Decoded data using concise binary object CBOR.
function decoded_test() {
    let decoded = null
    const startTime2 = Date.now()
    // for (let j = 0; j < 500; j++) {
        decoded = CBOR.decode(encoded);
    // }
    // print(`encoded result ${decoded.length}`)
    if (decoded.length == null) {
        throw new Error('Inconsistent with expected results')
    }
    const endTime2 = Date.now()
    print(`cborjs_Decode: ${endTime2 - startTime2} ms`)
}


function testCbor(initial) {

    let arr = new Array(10)
    let len = arr.length
    let start = Date.now()
    for (let i = 0; i < 2000; i++) {
        arr[i % len] = CBOR.encode(initial)
    }
    let end = Date.now()
    print ("cborjs_encode[" + typeof initial + "]: " + (end - start)  + ' ms' )

    let str = CBOR.encode(initial)
    let arr1 = new Array(10)
    let len1 = arr.length
    let start1 = Date.now()
    for (let i = 0; i < 2000; i++) {
        arr1[i % len] = CBOR.decode(str)
    }
    let end1 = Date.now()
    print ("cborjs_decode[" + typeof initial + "]: " + (end1 - start1)  + ' ms')

}

encoded_test()
decoded_test()
testCbor("test discription")
testCbor(121235465465421215465354565435)
let obj = new Object()
for (let i = 0; i < 50; i++) {
    obj["key" + i] = i
}
testCbor(obj)